﻿using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Kauf
{
    //TODO: bunu da Transfer.cs ve YeniAyrim.cs ye benzet!

    public partial class CikisEmri : XPopupForm
    {
        public CikisEmri()
        {
            InitializeComponent();
        }

        public int CikisEmriId { get; set; }
        public int TelaCikisEmriId { get; set; }
        public int AtelyeId { get; set; }

        string RollIds, Idx, DosyaNo, Article, SpName, Foys;
        int BrandId, CustomerId,SezonId;
        bool Tela;
        public CikisEmri(string article, string foys, string rollIds, string idx, int brandId,int sezonId, string dosyaNo, int customerId, bool tela, string spName,int atelyeId)
            : this()
        {
            Article = article;
            Foys = foys;
            RollIds = rollIds;
            Idx = idx;
            BrandId = brandId;
            SezonId = sezonId;
            DosyaNo = dosyaNo;
            CustomerId = customerId;
            Tela = tela;
            SpName = spName;
            AtelyeId = atelyeId;
        }

        private void CikisEmri_Load(object sender, EventArgs e)
        {
            GetDetails("SODetay", "GetOrderRef");

            gleOrderRef.Properties.Assign(GetIntellisensedRI(ds.SODetay));
            gleOrderRef.Properties.ProcessNewValue += new ProcessNewValueEventHandler(ri_ProcessNewValue2);
            gleOrderRef.Properties.View.Columns["BrandId"].ColumnEdit = RI(Rp.Brand);
            gleOrderRef.Properties.View.Columns["SezonId"].ColumnEdit = RI(Rp.Sezon);
            gleOrderRef.EditValueChanged += new EventHandler(gleOrderRef_EditValueChanged);

            gleBrand.Properties.Assign(RI(Rp.Brand));
            gleSezon.Properties.Assign(RI(Rp.Sezon));

            gle.Properties.Assign(RI(Rp.Atelye));

            DataTable dt = AppSession.Lookups.HareketTipi.Select("HareketTipiId in (10,11,13,14,15)").CopyToDataTable();
            RepositoryItemGridLookUpEdit riCikisTipi = GetCustomGLE(dt, "Name", "HareketTipiId");
            gleCikisTipi.Properties.Assign(riCikisTipi);
            gleCikisTipi.EditValueChanged += gleCikisTipi_EditValueChanged;
            gleCikisTipi.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;

            gleCikisTipi.EditValue = 10;
            gleBrand.EditValue = BrandId;
            gleSezon.EditValue = SezonId;
            txtDosyaNo.Text = DosyaNo;

            cmbTip.SelectedIndexChanged += cmbTip_SelectedIndexChanged;
            cmbTip.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.Shown += CikisEmri_Shown;

            if (AtelyeId > 0)
            {
                cmbTip.SelectedIndex = 0;
                gle.EditValue = AtelyeId;
            }
        }

        void gleCikisTipi_EditValueChanged(object sender, EventArgs e)
        {
            cmbTip.SelectedIndex = intParse(gleCikisTipi.EditValue) == 10 ? 0 : 1;
            UretimPanel.Enabled = intParse(gleCikisTipi.EditValue) == 10;
        }

        void CikisEmri_Shown(object sender, EventArgs e)
        {
            gle.Focus();
        }

        void cmbTip_SelectedIndexChanged(object sender, EventArgs e)
        {
            gle.EditValue = null;

            switch (cmbTip.SelectedIndex)
            {
                case 0:
                    gle.Properties.Assign(RI(Rp.Atelye));
                    break;
                case 1:
                    gle.Properties.Assign(RI(Rp.V_Customer));
                    break;
                case 2:
                    gle.Properties.Assign(RI(Rp.Vendor));
                    break;
            }
        }

        void gleOrderRef_EditValueChanged(object sender, EventArgs e)
        {
            if (gleOrderRef.Properties.View.GetFocusedRow() != null)
            {
                DataRow dr = ((DataRowView)gleOrderRef.Properties.View.GetFocusedRow()).Row;
                gleBrand.EditValue = intParse(dr["BrandId"]);
                gleSezon.EditValue = intParse(dr["SezonId"]);
                gleBrand.Properties.ReadOnly = true;
                gleSezon.Properties.ReadOnly = true;
            }
        }


        private void btnOk_Click(object sender, EventArgs e)
        {
            if (intParse(gleCikisTipi.EditValue) == 10)
            {
                if (intParse(gleBrand.EditValue) == 0)
                {
                    XtraMessageBox.Show("Marka boş olamaz!");
                    return;
                }
                if (intParse(gleSezon.EditValue) == 0)
                {
                    XtraMessageBox.Show("Sezon boş olamaz!");
                    return;
                }
            }

            string Atolye = "";
            byte[] bytes = null;

            if (gle.EditValue.ToInt() > 0)
                Atolye = AppSession.Lookups.Atelye.FindByAtelyeId(intParse(gle.EditValue)).Name;

            if (Tela)
            {
                DataTable dt = ExecuteDataTable("BlockRollOrtakKumas", RollIds, Idx, gleOrderRef.EditValue, intParse(gleBrand.EditValue), intParse(gleSezon.EditValue), intParse(gleCikisTipi.EditValue), intParse(gle.EditValue), txtNot.Text, WhoAmI, txtDosyaNo.Text, cmbTip.SelectedIndex, bytes);
                TelaCikisEmriId = (int)dt.Rows[0][0];
                string command = string.Format("SHOW ConfExpert.Forms.Depo.CikisEmirleri {0}", TelaCikisEmriId);
                Mail.Send("riza.demir@karbel.com", "Çıkış Talebi", string.Format("{0} Id'li  çıkış talebi oluşturuldu.", TelaCikisEmriId), "", command);
                Session.InvalidateStoklar = true;
            }
            else
            {
                OrtakKumasRaporu okr = new OrtakKumasRaporu(Article, RollIds, Idx, Atolye, CustomerId, SpName,false);

                MemoryStream stream = new MemoryStream();
                okr.CreateDocument();
                okr.PrintingSystem.SaveDocument(stream);
                bytes = stream.ToArray();

                string blockSp = SpName.Replace("Create", "BlockRoll");
                DataTable dt = ExecuteDataTable(blockSp, Article, Foys, RollIds, Idx, gleOrderRef.EditValue, intParse(gleBrand.EditValue), intParse(gleSezon.EditValue), intParse(gleCikisTipi.EditValue), intParse(gle.EditValue), txtNot.Text, WhoAmI, txtDosyaNo.Text, cmbTip.SelectedIndex, bytes);

                CikisEmriId = (int)dt.Rows[0][0];
                string command = string.Format("SHOW ConfExpert.Forms.Depo.CikisEmirleri {0}", CikisEmriId);
                Mail.Send("riza.demir@karbel.com", "Çıkış Talebi", string.Format("{0} Id'li  çıkış talebi oluşturuldu.", CikisEmriId), "", command);
                Session.InvalidateStoklar = true;

            }

            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
}